from dulwich.porcelain import status
from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q

from app.settings import settings
from app.controllers.content.content import content_controller
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.content import *
from app.utils.upload import upload_image,upload_file
from typing import List
from app.core.dependency import DependCustomerAuth

mini_contents_router = APIRouter()


@mini_contents_router.get("/list", summary="素材列表")
async def list_content(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        title: str = Query(None, description="素材名称"),
        tags: str = Query(None, description="标签名称"),
        type: str = Query(None, description="类型"),
        parent_id: int = Query(0, description="分类ID"),
):
    q = Q(state=True)
    if title:
        q &= Q(title__contains=title)
    if tags:
        q &= Q(tags__contains=tags)
    if type:
        q &= Q(type=type)
    if parent_id:
        q &= Q(parent_id=parent_id)
    total, data = await content_controller.content_list(page=page, page_size=page_size, search=q)
    for item in data:
        item["parent"] = item["parent_id"]
        item["type_name"] = settings.CONTENT_TYPE[item["type"]]
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@mini_contents_router.get("/get", summary="查看素材")
async def get_content(
        data_id: int = Query(..., description="素材ID"),
):
    data = await content_controller.content_get(data_id=data_id)
    return Success(data=data)
